package com.oneonefive.educationsystem.controller;

import com.oneonefive.educationsystem.domain.LabRepair;
import com.oneonefive.educationsystem.domain.Login;
import com.oneonefive.educationsystem.dto.Result;
import com.oneonefive.educationsystem.service.LabRepairService;
import com.oneonefive.educationsystem.vo.LabRepairVo;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.time.LocalDate;

@RestController
@CrossOrigin
@RequestMapping("/labRep")
public class LabRepairController {
    @Resource
    LabRepairService labRepairService;

    @PostMapping()
    @PreAuthorize("hasAuthority('teacher')")
    public Result labRepSave(@RequestBody LabRepair labRepair) {
        Result legitimacy = labRepairService.legitimacy(labRepair);
        if (!legitimacy.getSuccess()) {
            return legitimacy;
        }
        LocalDate currentDate = LocalDate.now();
        labRepair.setDate(Date.valueOf(currentDate));
        boolean success = labRepairService.save(labRepair);
        if (success) {
            return Result.ok("LabRepair新增成功");
        }
        return Result.fail("LabRepair新增失败");
    }

    //data:LIst<LabRepair>
    // 给管理员和实验员查维修表
    @PostMapping("/1")
    @PreAuthorize("hasAuthority('tester')")
    public Result labRepList(@RequestBody LabRepairVo labRepairVo,Authentication authentication) {
        if (labRepairVo.getPage() == null) {
            return Result.fail("请给出当前页数");
        }
        Login principal = (Login) authentication.getPrincipal();
        return labRepairService.labRepList(labRepairVo, principal.getUser());
    }

    //给老师查维修表
    @PostMapping("/forTea")
    @PreAuthorize("hasAuthority('teacher')")
    public Result labRepForTeaList(@RequestBody LabRepairVo labRepairVo,Authentication authentication) {
        if (labRepairVo.getPage() == null) {
            return Result.fail("请给出当前页数");
        }
        Login principal = (Login) authentication.getPrincipal();
        return labRepairService.labRepForTeaList(labRepairVo, principal.getUser());
    }

    @PutMapping
    @PreAuthorize("hasAuthority('tester')")
    public Result stateChangeTo1(@RequestBody LabRepair labRepair) {
        return labRepairService.stateChangeTo1(labRepair);
    }

}
